home *** CD-ROM | disk | FTP | other *** search
- package sub_arctic.output;
-
- import sub_arctic.lib.sub_arctic_error;
-
- import java.awt.Color;
- import java.awt.image.RGBImageFilter;
- import java.awt.Image;
- import java.lang.InternalError;
- import java.awt.image.PixelGrabber;
- import java.lang.InterruptedException;
- import java.awt.image.ImageObserver;
- import java.awt.Component;
- import java.awt.MediaTracker;
- import java.applet.Applet;
-
- /**
- * An RGBImageFilter for building images from an intensity mask and a color.
- *
- * @see java.awt.image.RGBImageFilter
- * @see sub_arctic.output.loaded_image#image_from_intensity_map
- * @author Ian Smith
- */
- public class mask_filter extends RGBImageFilter {
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /**
- * H of HSV representation of target. Range is [0,360)
- * and -1.0 if H is undefined.
- */
- protected double target_h;
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /**
- * S of HSV representation of target. Range is [0,1)
- */
- protected double target_s;
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /**
- * V of HSV representation of target. Range is [0,1)
- */
- protected double target_v;
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /**
- * Convert an RGB value to RGB. This is section 13.3.4 of
- * Foley & Van Dam.
- */
- protected void to_hsv(Color c) {
- double r,g,b; // range [0,1]
- int int_r=c.getRed(), int_g=c.getGreen(), int_b=c.getBlue();
- double max, min, delta;
-
- r=((double)int_r/255.0);
- g=((double)int_g/255.0);
- b=((double)int_b/255.0);
-
- /* compute max */
- max=r;
- if (g>max) max=g;
- if (b>max) max=b;
-
- /* compute min */
- min=r;
- if (g<min) min=g;
- if (b<min) min=b;
-
- target_v=max; // got our v
-
- // work on saturation
- if (max!=0.0) {
- target_s=(max-min)/max;
- } else {
- target_s=0; // black's saturation is zero
- }
-
- //work on hue
- if (target_s==0.0) {
- target_h=-1.0; // undefined hue, achromatic
- } else {
-
- // its chromatic
- delta=max-min;
-
- // is it between yellow and magenta
- if (r==max) {
- target_h=(g-b)/delta;
- } else if (g==max) { // between cyan and yellow?
- target_h=2.0+(b-r)/delta;
- } else { // must be between magenta and cyan
- target_h=4.0 + (r-g)/delta;
- }
-
- /* convert hue to degrees */
- target_h=target_h*60.0;
- /* make sure its positive */
- if (target_h<0) {
- target_h+=360.0;
- }
- }
- }
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /**
- * This is temporary holding place for the R value when we
- * are doing a conversion from HSV to RGB.
- */
- protected int tmp_r;
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /**
- * This is temporary holding place for the G value when we
- * are doing a conversion from HSV to RGB.
- */
- protected int tmp_g;
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /**
- * This is temporary holding place for the B value when we
- * are doing a conversion from HSV to RGB.
- */
- protected int tmp_b;
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /**
- * Convert our HSV representation into a color with a
- * different v value.
- *
- * @param double v the new v value
- */
- protected void new_color(double v) {
- double h=target_h, s=target_s,i,f,p,q,t;
- int which;
-
- /* is it achromatic ? */
- if (s==0) {
- if (h!=-1.0) {
- throw new sub_arctic_error("Possible problem in HSV " +
- "conversion routine");
- }
-
- /* its a greyscale, just make it from the value */
- tmp_r=(int)(Math.round(v * 255.0));
- tmp_g=tmp_r;
- tmp_b=tmp_r;
-
- } else {
-
- /* it has a hue */
- /* validity check on 360 */
- if (h==360.0) h=0.0;
- h/=60.0;
- i=Math.floor(h);
- f=h-i;
- p=v*(1-s);
- q=v*(1-(s*f));
- t=v*(1-(s*(1-f)));
- which=(int)i;
-
- switch (which) {
- case 0:
- tmp_r=(int)Math.round(v*255.0);
- tmp_g=(int)Math.round(t*255.0);
- tmp_b=(int)Math.round(p*255.0);
- break;
- case 1:
- tmp_r=(int)Math.round(q*255.0);
- tmp_g=(int)Math.round(v*255.0);
- tmp_b=(int)Math.round(p*255.0);
- break;
- case 2:
- tmp_r=(int)Math.round(p*255.0);
- tmp_g=(int)Math.round(v*255.0);
- tmp_b=(int)Math.round(t*255.0);
- break;
- case 3:
- tmp_r=(int)Math.round(p*255.0);
- tmp_g=(int)Math.round(q*255.0);
- tmp_b=(int)Math.round(v*255.0);
- break;
- case 4:
- tmp_r=(int)Math.round(t*255.0);
- tmp_g=(int)Math.round(p*255.0);
- tmp_b=(int)Math.round(v*255.0);
- break;
- case 5:
- tmp_r=(int)Math.round(v*255.0);
- tmp_g=(int)Math.round(p*255.0);
- tmp_b=(int)Math.round(q*255.0);
- break;
- default:
- System.out.println("Error in HSV->RGB conversion");
- tmp_r=0;
- tmp_g=0;
- tmp_b=0;
- }
- /* done with it*/
- }
- }
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /*
- * Construct a new mask filter, given some color. This will
- * filter the image passed through it as an intensity map
- * of the color supplied here. Pixels in the intensity map
- * which are lower than transp_intensity are made transparent.
- *
- * @param Color c the target color.
- * @param int transp_intensity this intensity or lower will be turned to
- * transparent.
- */
- public mask_filter(Color c,int transp_intensity) {
- to_hsv(c);
- // The filter's operation does not depend on the
- // pixel's location, so IndexColorModels can be
- // filtered directly.
- canFilterIndexColorModel = true;
- transp_color=transp_intensity;
- }
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /**
- * Store the intensity we are using for transparent.
- */
- protected int transp_color;
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /**
- * This function gets called for each pixel.
- * @param int x the x coord of the pixel (not used)
- * @param int y the y coord of the pixel (not used)
- * @param int rgb the rgb value (plus the alpha channel)
- * @return int the new value
- */
- public int filterRGB(int x, int y, int rgb) {
- /* we assume that all of r g & b are equal */
- int intensity=rgb & 0xff, alpha = rgb & 0xff000000;
- double scale= ((double)intensity/255.0);
-
- if (intensity<transp_color) {
- ///* transparent */
- alpha=0;
- tmp_r=0;
- tmp_g=0;
- tmp_b=0;
- } else {
- /* do an RGB conversion scaling the V by scale*/
- new_color(target_v*scale);
- }
- return ( alpha |
- (tmp_b) |
- (tmp_g << 8) |
- (tmp_r << 16));
- }
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- }
- /*=========================== COPYRIGHT NOTICE ===========================
-
- This file is part of the subArctic user interface toolkit.
-
- Copyright (c) 1996 Scott Hudson and Ian Smith
- All rights reserved.
-
- The subArctic system is freely available for most uses under the terms
- and conditions described in
- http://www.cc.gatech.edu/gvu/ui/sub_arctic/sub_arctic/doc/usage.html
- and appearing in full in the lib/interactor.java source file.
-
- The current release and additional information about this software can be
- found starting at: http://www.cc.gatech.edu/gvu/ui/sub_arctic/
-
- ========================================================================*/
-